﻿
/****************************************************  Common Function  ***********************************************************/

var showContextMenu = true;
this.document.oncontextmenu = function() { return showContextMenu; }

function getBaseUrl() {
    return window.location.protocol + "//" + window.location.hostname +
        ":" + window.location.port;
}

function openMenu(url) {
    viewContent = applyIf(parent.document.getElementById("Content"), document.getElementById("Content"));
    if (viewContent)
        viewContent.src = url;
    //return false;
    //    var viewContent = applyIf(parent.window.frames['ViewContent'], window.frames['ViewContent']);
    //    if (!viewContent) {
    //        viewContent = parent.document.getElementById("ViewContent");
    //        if(viewContent )
    //        viewContent.src = url;
    //    }
    //    else{
    //        viewContent.location.href = url;
    //        return true;
    //    } 
    //    return false;
}

function applyIf(a, b) {
    return a ? a : b;
}




function openPage(url, full) {
    if (full)
        openPage_full(url);
    else
        window.open(url);
    // window.open(url,'_blank','a');
}

//function openPage_full(url){    
//    var w = window.screen.availWidth-10;
//    var h =window.screen.availHeight-10;
//   InstantASP_OpenWindow(url, w, h,1,0,0,0,0,1,false);
//   // window.open(url,'maxwindow','');
// //window.open(url,'maxwindow', 'fullscreen');
//   // window.open(url,'maxwindow','toolbar=no,location=no,directories=no,menubar=yes,scrollbars=yes,resizable=no,status=no'); 
//}

function openPage_full(url) {
    var w = screen.width;
    var h = screen.height;
    newwin = window.open("", "", "toolbar=no,menubar=no,location=no, resizable=no,status=no,scrollbars=yes,top=0,left=0,width=" + (w - 10) + ",height=" + (h - 50));
    if (document.all) {
        //newwin.moveTo(0, 0);
        newwin.resizeTo(screen.width, screen.height);
    }
    newwin.location = url;
    newwin.focus();
}

function openPage_size(url, w, h) {
    InstantASP_OpenWindow(url, w, h, 1, 0, 0, 0, 0, 1, false);
}

function loadPage(url) {
    window.location = url;
}

function goHomePage(a) {
    var url = getBaseUrl();
    if (a)
        openPage(url);
    else
        loadPage(url);
}

function refresh() {
    window.location.reload();
}

function QueryString() {
    var name, value, i;
    var str = location.href;
    var num = str.indexOf("?")
    str = str.substr(num + 1);
    var arrtmp = str.split("&");
    var query = {};
    for (i = 0; i < arrtmp.length; i++) {
        num = arrtmp[i].indexOf("=");
        if (num > 0) {
            name = arrtmp[i].substring(0, num);
            value = arrtmp[i].substr(num + 1);
            query[name] = value;
        }
    }
    return query;
}

function page_cancel() {
    var str = location.href;
    var num = str.indexOf("?")
    if (num > 0)
        str = str.substr(0, num);
    location.href = str;
}

function page_exit() {
    window.close();
}

function page_new() {
    var str = location.href;
    var num = str.indexOf("?")
    if (num > 0)
        str = str.substr(0, num);
    openPage_full(str);
}

function commonkeydown() {
    if (event.keyCode == 13)
        event.keyCode = 9;
}


/*Call iframe's function*/
function injectIframe(func, frameName) {
    var cs = document.all;
    if (frameName) {
        var frame = document.frames[frameName];
        if (frame) {
            if (frame[func]) {
                if (typeof frame[func] == 'function') {
                    frame[func]();
                }
            }
        }
    }
    else {
        for (var i = 0; i < cs.length; i++) {
            if (cs[i].tagName.toUpperCase() == "IFRAME") {
                var frame = document.frames[cs[i].name];
                if (frame) {
                    if (frame[func]) {
                        if (typeof frame[func] == 'function') {
                            frame[func]();
                            return;
                        }
                    }
                }
            }
        }
    }
}

/* 
function assignValue(json)
{
var formlist = document.all.tags('form');			
for (j=0; j<formlist.length; j++)
{
var theform=formlist[j];		
for (i=0;i<theform.elements.length;i++) 
{
var e = theform.elements[i];
if(chkAutoAssignEnable(e.id))
{
var name = getMainName (e.id);
if(name!="")
if(existKey (json,name))
$(e.id).value= json[name.toUpperCase()];
}			 
}
}
}

function clearAll()
{
var formlist = document.all.tags('form');			
for (j=0; j<formlist.length;j++)
{
var theform=formlist[j];		
for (i=0;i<theform.elements.length;i++) 
{
var e = theform.elements[i];
if(chkAutoAssignEnable(e.id))
{			    
$(e.id).value="";
}			 
}
}
}

function existKey(json, key)
{
for(var e in json)
{
if(compare(e,key))
return true;
}
return false;
    
}
*/

function chkAutoAssignEnable(ctrlName) {
    var prefixes = ['txt', 'cbo'];
    if (ctrlName.length > 3)
        for (var i = 0; i < prefixes.length; i++)
        if (ctrlName.substr(0, 3) == prefixes[i]) return true;
    return false;
}

function getMainName(ctrlName) {
    if (ctrlName.length > 3)
        return ctrlName.substr(3);
    return "";
}

function isNullOrEmpty(str) {
    return str == null || str == "";
}

function compare(str1, str2) {
    return str1.toLowerCase() == str2.toLowerCase();
}

var coolcode_oldColor;

function selectAll(sender, target) {
    var inputs = document.all.tags("INPUT");
    var chkflg = document.getElementById(sender).checked;
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox" && inputs[i].id.indexOf(target) >= 0) {
            inputs[i].checked = chkflg;
            //            inputs[i].onclick();
        }
    }
}
function highlightSelected(chkSelected, c1, c2) {
    window.event.cancelBubble = true;
    //    chkSelected.parentElement.parentElement.style.backgroundColor = chkSelected.checked ? c2 : c1;
}
function mouseOver(obj) {
    coolcode_oldColor = obj.style.backgroundColor;
    obj.style.backgroundColor = '#D1DDF1';
    obj.style.cursor = 'hand';
}
function mouseOut(obj) {
    obj.style.backgroundColor = coolcode_oldColor;
}


// -------------------- 将Enter替换成Tab --------------------
this.document.onkeydown = function(evt) {
    var isie = (document.all) ? true : false;
    var key;
    var srcobj;
    if (isie) {
        key = event.keyCode;
        srcobj = event.srcElement;
    }
    else {
        key = evt.which;
        srcobj = evt.target;
    }
    if (key == 13 && srcobj.type != 'button' && srcobj.type != 'submit' && srcobj.type != 'reset' && srcobj.type != 'textarea' && srcobj.type != '') {
        if (isie) {
            event.keyCode = 9;
        }
        else {
            var el = getNextElement(evt.target);
            if (el.type != 'hidden')
                el.focus();
            else {
                while (el.type == 'hidden')
                    el = getNextElement(el);
                el.focus();
            }
            return false;
        }
    }
}

function getNextElement(field) {
    var form = field.form;
    for (var e = 0; e < form.elements.length; e++) {
        if (field == form.elements[e])
            break;
    }
    return form.elements[++e % form.elements.length];
}

// -------------------- 禁止页面嵌入到框架中 --------------------
function setToTop() {
    if (top.location.href != document.location.href) {
        top.location.href = document.location.href;
    }
}

function getClientBounds() {
    var clientWidth;
    var clientHeight;
    switch (Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}

function resizeElements() {
    var clientBounds = getClientBounds();
    var clientWidth = clientBounds.width;
    var clientHeight = clientBounds.height;
    var bg = $get("modalBackground");
    var dlg = $get("DialogFrame");
    if (bg != null) {
        bg.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
        bg.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
    }
    if (dlg != null) {
        dlg.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
        dlg.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
    }
}

function initBackground() {
    document.write('<iframe id="DialogFrame" frameborder="0" style="display:none;"></iframe>');
    $addHandler(window, "scroll", resizeElements);
    $addHandler(window, "resize", resizeElements);
    resizeElements();
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title, message, type, autohide) {
    if (!type) {
        type = 'error';
    }

    var dlg = document.getElementById("DialogFrame");
    var dlgdoc = dlg.contentWindow.document;
    var dlgheader;
    var dlglink;

    var dialog;
    var dialogheader;
    var dialogclose;
    var dialogtitle;
    var dialogcontent;
    var dialogmask;

    dlgheader = dlgdoc.createElement('head');
    dlgdoc.body.appendChild(dlgheader);

    dlglink = document.createElement('link');
    dlglink.setAttribute("rel", "stylesheet");
    dlglink.setAttribute("type", "text/css");
    dlglink.setAttribute("href", "/Content/css/Dialog.css");
    dlg.contentWindow.getElementsByTagName("head")[0].appendChild(dlglink);

    //alert(dlg.contentWindow);
    //dlgdoc.body.className = 'dialog - mask';

    //    margin: 0px;
    //    background-position: #808080;
    //    background: #808080;
    //    filter: Alpha(Opacity=70);
    //    opacity: 0.7;


    //    if (!dlg.document.getElementById('dialog')) {
    dialog = dlgdoc.createElement('div');
    dialog.id = 'dialog';
    dialog.innerHTML = 'adalfasdflasflasf';
    //        dialogheader = dlg.document.createElement('div');
    //        dialogheader.id = 'dialog-header';
    //        dialogtitle = dlg.document.createElement('div');
    //        dialogtitle.id = 'dialog-title';
    //        dialogclose = dlg.document.createElement('div');
    //        dialogclose.id = 'dialog-close'
    //        dialogcontent = dlg.document.createElement('div');
    //        dialogcontent.id = 'dialog-content';
    //        dialogmask = dlg.document.createElement('div');
    //        dialogmask.id = 'dialog-mask';
    //        dlg.document.body.appendChild(dialogmask);
    dlgdoc.body.appendChild(dialog);
    //        dialog.appendChild(dialogheader);
    //        dialogheader.appendChild(dialogtitle);
    //        dialogheader.appendChild(dialogclose);
    //        dialog.appendChild(dialogcontent); ;
    //        dialogclose.setAttribute('onclick', 'hideDialog()');
    //        dialogclose.onclick = hideDialog;
    //    } else {
    //        dialog = document.getElementById('dialog');
    //        dialogheader = document.getElementById('dialog-header');
    //        dialogtitle = document.getElementById('dialog-title');
    //        dialogclose = document.getElementById('dialog-close');
    //        dialogcontent = document.getElementById('dialog-content');
    //        dialogmask = document.getElementById('dialog-mask');
    //        dialogmask.style.visibility = "visible";
    //        dialog.style.visibility = "visible";
    //    }
    //    dialog.style.opacity = .00;
    //    dialog.style.filter = 'alpha(opacity=0)';
    //    dialog.alpha = 0;
    //    var width = pageWidth();
    //    var height = pageHeight();
    //    var left = leftPosition();
    //    var top = topPosition();
    //    var dialogwidth = dialog.offsetWidth;
    //    var dialogheight = dialog.offsetHeight;
    //    var topposition = top + (height / 3) - (dialogheight / 2);
    //    var leftposition = left + (width / 2) - (dialogwidth / 2);
    //    dialog.style.top = topposition + "px";
    //    dialog.style.left = leftposition + "px";
    //    dialogheader.className = type + "header";
    //    dialogtitle.innerHTML = title;
    //    dialogcontent.className = type;
    //    dialogcontent.innerHTML = message;
    //    var content = document.getElementById(WRAPPER);
    //    //dialogmask.style.height = content.offsetHeight + 'px';
    //    dialog.timer = setInterval("fadeDialog(1)", TIMER);
    //    if (autohide) {
    //        dialogclose.style.visibility = "hidden";
    //        window.setTimeout("hideDialog()", (autohide * 1000));
    //    } else {
    //        dialogclose.style.visibility = "visible";
    //    }
    dlg.style.display = "";
}




/* ----------------------------------------------------------- */
// Browser Sniff
/* ----------------------------------------------------------- */

var InstantASP_UserAgent = navigator.userAgent.toLowerCase();
var InstantASP_Opera = (InstantASP_UserAgent.indexOf('opera') != -1); // is opera
var InstantASP_Opera8 = ((InstantASP_UserAgent.indexOf('opera 8') != -1 || InstantASP_UserAgent.indexOf('opera/8') != -1) ? 1 : 0); // is opera8
var InstantASP_NS4 = (document.layers) ? true : false; // is netscape 4
var InstantASP_IE4 = (document.all && !document.getElementById) ? true : false; // is IE 4
var InstantASP_IE5 = (document.all && document.getElementById) ? true : false; // is IE 5+
var InstantASP_NS6 = (!document.all && document.getElementById) ? true : false; // is netscape 6
var InstantASP_FireFox = (InstantASP_UserAgent.indexOf("firefox/") != -1); // is firefox
var InstantASP_Transitions = (InstantASP_IE5 || InstantASP_IE4) ? true : false // do we support transitions

/* ----------------------------------------------------------- */
// Opens a new window
/* ----------------------------------------------------------- */

function InstantASP_OpenWindow(Url, Width, Height, Scroll, ToolBar, Location, Status, MenuBar, Resizeable, Unique) {
    var String;
    var winName = Width.toString() + Height.toString()
    String = "toolbar=" + ToolBar + ",location=" + Location
    String += ", directories=0,status=" + Status + ",menubar=" + MenuBar + ","
    String += "scrollbars=" + Scroll + ",resizable=" + Resizeable + ",copyhistory=0,";
    String += ",width=";
    String += Width;
    String += ",height=";
    String += Height;

    // should we center popup window
    if (InstantASP_IE4 || InstantASP_IE5 || InstantASP_Opera || InstantASP_NS6) {
        WndTop = (screen.height - Height) / 2;
        WndLeft = (screen.width - Width) / 2;
        String += ",top=";
        String += WndTop;
        String += ",left=";
        String += WndLeft;
    }

    // should we display single popup or allow multiple
    try {
        if (Unique == true) { WinPic = window.open(Url, WinNum++, String); }
        else { WinPic = window.open(Url, winName, String); WinPic.focus(); }
    }
    catch (e) { };
}
